﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbolandiaBL
{

    internal class UIProductProvider
    {
        private ArbolandiaDBDataContext _db;

        public UIProductProvider()
        {
            _db = new ArbolandiaDBDataContext();
        }

        internal UIProductDetail GetProductDetail(int idproduct)
        {
            var res = new List<UIProductDetail>();
            //Obtenemos Lista de Productos
            var query = _db.Product_Product.Where(w => w.Id == idproduct);
            foreach (var item in query)
            {

                var addobj = new UIProductDetail();
                addobj.SubCategoryId = (Int32)CompiledQuerys.GetSubCategoryByProductId(_db, idproduct);
                addobj.Detalles = item.Detalles;
                addobj.DiscountPrice = "";
                addobj.Id = item.Id;
                addobj.IsDestacado = (bool)item.IsDestacado;
                addobj.IsOferta = (bool)item.IsOferta;
                addobj.LongDescription = item.DescripcionLarga;
                addobj.Mantenimiento = item.Mantenimiento;
                addobj.MesesFloracion = item.MesesFloracion;
                addobj.MesesFrutos = item.MesesFruto;
                addobj.Name = item.Nombre;
                addobj.Plantacion = item.Plantación;
                addobj.ScientistName = item.NombreCientifico;
                addobj.ShortDescription = item.DescripcionCorta;
                addobj.ShowAtHome = (bool)item.ShowAtHome;
                addobj.Size = item.AlturaPlanta;
                //Obtenemos las imagenes.
                var imagesquery = _db.spGetImagesProduct(item.Id).ToList();
                foreach (var it in imagesquery.Where(w => w.isThumbnail == true))
                {
                    addobj.ThumbnailsImages.Add(new UIImageProduct()
                    {
                        GuidId = it.guidId.ToString(),
                        ImageUrl = it.imageUrl,
                        iOrder = it.iorder,
                        LinkUrl = it.LinkUrl,
                        ToolTip = it.ToolTip
                    });
                }
                foreach (var it in imagesquery.Where(w => w.isThumbnail == false))
                {
                    addobj.FullSizeImages.Add(new UIImageProduct()
                    {
                        GuidId = it.guidId.ToString(),
                        ImageUrl = it.imageUrl,
                        iOrder = it.iorder,
                        LinkUrl = it.LinkUrl,
                        ToolTip = it.ToolTip
                    });
                }
                //Obtenemos los complementos
                var complementquery = from q in _db.spGetProductComplements(item.Id, false)
                                      select q;
                foreach (var c in complementquery)
                {
                    addobj.Complements.Add(new UIProductResume()
                    {
                        Id = c.IdRef,
                        ImageUrl = c.url,
                        LowerPrice = (decimal)c.lowerPrice,
                        Name = c.Name
                    });
                }
                //obtenemos Detalles de Productos ( Precios )
                var detailpricequery = from d in _db.Product_ProductDetail
                                       where d.Product_ProductId == item.Id
                                       select d;
                foreach (var dp in detailpricequery)
                {
                    addobj.ProductPriceDetails.Add(new UIProductPriceDetail()
                    {
                        Description = dp.Descripcion,
                        DiscountPrice = 0,
                        GuidId = dp.GuidId.ToString(),
                        Id = dp.Id,
                        IsAcompanamiento = dp.IsAcompanamiento != null ? (bool)dp.IsAcompanamiento : false,
                        Price = dp.Precio != null ? (decimal)dp.Precio : 0,
                        ProductCode = dp.CodigoProducto,
                        Stock = dp.Stock != null ? (int)dp.Stock : 0
                    });
                }
                //obtenemos las preferencias
                var preferencequery = from p in _db.spGetProductPreferences(item.Id)
                                      select p;
                foreach (var pq in preferencequery)
                {
                    addobj.Preferences.Add(new UIProductPreference()
                    {
                        Description = pq.Description,
                        ImageUrl = pq.url,
                        Name = pq.Name,
                        Order = pq.Order != null ? (short)pq.Order : short.Parse("0")
                    });
                }
                res.Add(addobj);
                break;
            }

            return res.FirstOrDefault<UIProductDetail>();
        }

        internal UIProductVariousDetail GetProductVariousDetail(int idproduct)
        {
            var res = new List<UIProductVariousDetail>();
            //Obtenemos Lista de Productos
            var query = _db.Product_ProductVarious.Where(w => w.Id == idproduct);
            foreach (var item in query)
            {
                var addobj = new UIProductVariousDetail();
                addobj.SubCategoryId = (Int32)CompiledQuerys.GetSubCategoryByProductId(_db, idproduct);
                addobj.Detalles = item.Detalles;
                addobj.DiscountPrice = "";
                addobj.Id = item.Id;
                addobj.IsDestacado = (bool)item.IsDestacado;
                addobj.IsOferta = (bool)item.IsOferta;
                addobj.LongDescription = item.DescripcionLarga;
                addobj.Name = item.Nombre;
                addobj.ShortDescription = item.DescripcionCorta;
                addobj.ShowAtHome = (bool)item.ShowAtHome;
                //Obtenemos las imagenes.
                var imagesquery = _db.spGetImagesProduct(item.Id).ToList();
                foreach (var it in imagesquery.Where(w => w.isThumbnail == true))
                {
                    addobj.ThumbnailsImages.Add(new UIImageProduct()
                    {
                        GuidId = it.guidId.ToString(),
                        ImageUrl = it.imageUrl,
                        iOrder = it.iorder,
                        LinkUrl = it.LinkUrl,
                        ToolTip = it.ToolTip
                    });
                }
                foreach (var it in imagesquery.Where(w => w.isThumbnail == false))
                {
                    addobj.FullSizeImages.Add(new UIImageProduct()
                    {
                        GuidId = it.guidId.ToString(),
                        ImageUrl = it.imageUrl,
                        iOrder = it.iorder,
                        LinkUrl = it.LinkUrl,
                        ToolTip = it.ToolTip
                    });
                }
                //Obtenemos los complementos
                var complementquery = from q in _db.spGetProductComplements(item.Id, true)
                                      select q;
                foreach (var c in complementquery)
                {
                    addobj.Complements.Add(new UIProductResume()
                    {
                        Id = c.IdRef,
                        ImageUrl = c.url,
                        LowerPrice = (decimal)c.lowerPrice,
                        Name = c.Name
                    });
                }
                //obtenemos Detalles de Productos ( Precios )
                var detailpricequery = from d in _db.Product_ProductDetail
                                       where d.Product_ProductId == item.Id
                                       select d;
                foreach (var dp in detailpricequery)
                {
                    addobj.ProductPriceDetails.Add(new UIProductPriceDetail()
                    {
                        Description = dp.Descripcion,
                        DiscountPrice = 0,
                        GuidId = dp.GuidId.ToString(),
                        Id = dp.Id,
                        IsAcompanamiento = dp.IsAcompanamiento != null ? (bool)dp.IsAcompanamiento : false,
                        Price = dp.Precio != null ? (decimal)dp.Precio : 0,
                        ProductCode = dp.CodigoProducto,
                        Stock = dp.Stock != null ? (int)dp.Stock : 0
                    });
                }
                res.Add(addobj);
                break;
            }

            return res.FirstOrDefault<UIProductVariousDetail>();
        }
    }
}
